const query=require("../db/index")
const {md5}=require("utility")
const jwt=require("jsonwebtoken")
const {success,Err} =require("../moddle/index")
class loginList{
    zhuce=async(ctx)=>{
       const {usename,password}=ctx.request.body
       console.log(usename,password)
       let sql=`select * from ku where usename="${usename}"`
       const data=await query(sql)
       console.log(data.length)
       if(data.length){
          ctx.body=Err("已有账号")
          return
       }else{
           let sql=`insert into ku(usename,password) values("${usename}","${md5(String(password))}")`
           const data=await query(sql)
           if(data.affectedRows===1){
                 ctx.body=success("成功")
           }else{
               ctx.bofy=Err("失败")
           }
       }
    }
    login=async(ctx)=>{
        const{usename,password}=ctx.request.body
        console.log(usename,password)
        const sql=`select * from ku where usename="${usename}" and password="${md5(String(password))}"`
        const data=await query(sql) 
        const token=""
        if(data.length){
           ctx.body=success({
               data:data[0],
               token:jwt.sign({id:data[0].id},"111")
           })
        }else{
            ctx.body=Err("失败")
        }
    }
}
module.exports=new loginList()